#include <string.h>
#include "mcu.h"
#include "AnimationTable.h"
#include "Task.h"
#include "Task_LCfg.h"
#include "General.h"
#include "Tps929120.h"
#include "Tps929120_PBCfg.h"
#include "SysCommon.h"
#include "Driver.h"
#include "Process.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Variables
 ******************************************************************************/
 volatile PWM_Capture_HVstruct       PWM_Capture_HV = {0};
 volatile PWM_LightShow_struct		 PWM_LightShow = {0};
 volatile LAMB_Statusstruct 		 LAMB_Status = {0};
 volatile LAMB_Fender_Statusstruct   LAMB_Fender_Status = {0};

LinCMD_t g_stLinReqCmd = LIN_CMD_INIT_VALUE;

AnmPlayPara_t g_stAnmPlayPara = PLAYER_PARA_INIT_VALUE;

uint8_t g_u8CurWorkMode = LAMP_MODE_INIT;
uint8_t g_u8PreWorkMode = LAMP_MODE_INIT;
uint8_t g_u8ReqWorkMode = LAMP_MODE_INIT;

uint8_t TEST[200]={0,0,0,0};
 
static uint8_t s_u8Lamps_Status = OFF;
uint8_t	g_u8LS_Start_Wait = OFF;



uint16_t g_16SetChannel[16] = {0, 0, 0, 0, 
								 0, 0, 0, 0, 
								 0, 0, 0, 0, 
								 0, 0, 0, 0};

uint16_t g_16FenderSetChannel[3] = {0, 0, 0};

uint16_t g_16SetPwmDuty[16][12] = 
{
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};

uint16_t g_16FenderSetPwmDuty[3][12] = 
{
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
	{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

};

/*******************************************************************************
 * Static Functions
 ******************************************************************************/
static uint16_t bubbleSort(uint16_t *arr, uint8_t n)
{
	uint16_t temp = 0;
	uint16_t temp2 = 0;
    for (uint8_t i = 0; i < n-1; ++i) {
        for (uint8_t j = 0; j < n-i-1; ++j) {
            if (arr[j] > arr[j+1]) {
                // Swap elements
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }

    for (uint8_t i = 1; i < n-1; ++i)
    {
        temp2 = temp2 + arr[i];
    }
    return temp2/(n-2);
}

static void set_pwm_blocking(uint8_t dev, uint16_t pwm[])
{
	uint8_t pwmduty_8byte[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	uint8_t pwmduty_4byte[4] = {0x00, 0x00, 0x00, 0x00};

	for(uint8_t i=0; i<8; i++)
	{
		pwmduty_8byte[i] = ((pwm[i]>>4) & 0xFF);
	}

	for(uint8_t i=0; i<4; i++)
	{
		pwmduty_4byte[i] = ((pwm[i+8]>>4) & 0xFF);
	}
	Tps_WriteRegsBlocking(dev, REG_ADD_PWM0, pwmduty_8byte, eBurst_8_Byte, TRUE);
	Tps_WriteRegsBlocking(dev, REG_ADD_PWMx(8), pwmduty_4byte, eBurst_4_Byte, TRUE);
}

static void TPS_PWM_UPDATE(uint16_t pwm_arr[][12])
{
	static uint8_t TPS_Step_PWM = 0;
	uint8_t u8RegDataBuf[1] = {0};


#if CURRENT_PROJECT  == PROJECT_trunk

	if(TPS_Step_PWM == 0)
	{
		TPS_Step_PWM = 1;
		
		set_pwm_blocking(TPS_CHIP_ADD_5, pwm_arr[5]);
		set_pwm_blocking(TPS_CHIP_ADD_0, pwm_arr[0]);
		set_pwm_blocking(TPS_CHIP_ADD_1, pwm_arr[1]);
		set_pwm_blocking(TPS_CHIP_ADD_4, pwm_arr[4]);
		
	}
	else if(TPS_Step_PWM == 1)
	{
		//4pcs pl
		TPS_Step_PWM = 2;

		set_pwm_blocking(TPS_CHIP_ADD_7, pwm_arr[7]);
		set_pwm_blocking(TPS_CHIP_ADD_2, pwm_arr[2]);
		set_pwm_blocking(TPS_CHIP_ADD_3, pwm_arr[3]);
		set_pwm_blocking(TPS_CHIP_ADD_6, pwm_arr[6]);
		set_pwm_blocking(TPS_CHIP_ADD_8, pwm_arr[8]);

	}
	else if(TPS_Step_PWM == 2)
	{
		TPS_Step_PWM = 0;
		
		set_pwm_blocking(TPS_CHIP_ADD_13, pwm_arr[13]);
		set_pwm_blocking(TPS_CHIP_ADD_9, pwm_arr[9]);
		set_pwm_blocking(TPS_CHIP_ADD_12, pwm_arr[12]);
		set_pwm_blocking(TPS_CHIP_ADD_10, pwm_arr[10]);
		set_pwm_blocking(TPS_CHIP_ADD_11, pwm_arr[11]);
	}
	else
	{
		TPS_Step_PWM = 0;
	}
	
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))

	if(TPS_Step_PWM == 0)
	{
		TPS_Step_PWM = 1;
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 0, pwm_arr[1][0]);
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 1, pwm_arr[1][1]);
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 2, pwm_arr[1][2]);
	}
	if(TPS_Step_PWM == 1)
	{
		TPS_Step_PWM = 2;
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 3, pwm_arr[1][3]);
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 4, pwm_arr[1][4]);
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 5, pwm_arr[1][5]);
	}
	if(TPS_Step_PWM == 2)
	{
		TPS_Step_PWM = 0;
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 6, pwm_arr[1][6]);
		Tps_SetChannelPWMDuty(TPS_CHIP_ADD_1, 7, pwm_arr[1][7]);

	}

#endif
}


static void LampMode_Normal_Exit()
{
	if(g_stTaskList[TASK_INDEX_GENERAL].u32DelayTime > 10)
	{
		g_stTaskList[TASK_INDEX_GENERAL].u32DelayTime = 10;
	}
	Anm_Stop();
}

static void LampMode_Normal_Enter()
{
//    PWM_FLAG = PWM_NORMAL; //delete by ZQF 20240119
//      Light_ALLClose();
//      Anm_Stop();
}
static void LampMode_Normal_Handle()
{

}

static void LampMode_Welcome1_Exit()
{
	Anm_Stop();
}
static void LampMode_Welcome1_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_Welcome1_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmWelcome1 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmWelcome1;
		g_stAnmPlayPara.u16AnmTableSize = ANM_WEL1_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_Welcome2_Exit()
{
    Anm_Stop();
}
static void LampMode_Welcome2_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_Welcome2_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmWelcome2 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmWelcome2;
		g_stAnmPlayPara.u16AnmTableSize = ANM_WEL2_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_Welcome3_Exit()
{
    Anm_Stop();
}
static void LampMode_Welcome3_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_Welcome3_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmWelcome3 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmWelcome3;
		g_stAnmPlayPara.u16AnmTableSize = ANM_WEL3_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS1_Exit()
{
	Anm_Stop();
}
static void LampMode_LS1_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_LS1_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL1 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL1;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL1_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS2_Exit()
{
	Anm_Stop();
}
static void LampMode_LS2_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_LS2_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL2 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL2;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL2_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS3_Exit()
{
	Anm_Stop();
}
static void LampMode_LS3_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_LS3_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL3 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL3;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL3_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_OFF_Exit()
{
	Anm_Stop();
}

static void LampMode_OFF_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}

static void LampMode_LS4_Exit()
{
	Anm_Stop();
}
static void LampMode_LS4_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_LS4_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL4 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL4;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL4_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS5_Exit()
{
	Anm_Stop();
}
static void LampMode_LS5_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_LS5_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL5 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL5;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL5_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS6_Exit()
{
//	Anm_Stop();
}

static void LampMode_AcReq_Exit()
{
    Anm_Stop();
}


static void LampMode_LS8_Exit()
{
    Anm_Stop();
}

static void Update_Animation()
{
	uint8_t count = 0;
	PWM_LightShow.PWM_LS_Counter = 0;
	for(count = 0; count < 48; count = count + 6)
	{
		PWM_LightShow.DL.TEST[count] = 1;
		PWM_LightShow.DL.TEST[count+1] = 4;
		PWM_LightShow.DL.TEST[count+2] = 2;
		PWM_LightShow.DL.TEST[count+3] = 5;
		PWM_LightShow.DL.TEST[count+4] = 3;
		PWM_LightShow.DL.TEST[count+5] = 6;
	}
	PWM_LightShow.DL.TEST[48] = 1;
	PWM_LightShow.DL.TEST[49] = 0;
	PWM_LightShow.DL.TEST[50] = 0;
}

static void LampMode_LS6_Enter()
{
	g_u8P_S_Flag = ON;
	Anm_Finish();
//	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_LS6_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL6 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL6;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL6_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS7_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL7 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL7;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL7_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS8_Handle()
{
//	if(Anm_GetCurrentAnmTable() != g_stAnmSL8 || PWM_LS_Flag == ON)
//	{
//		Anm_Stop();
//		Anm_ResetPlayPara(&g_stAnmPlayPara);
//		g_stAnmPlayPara.pstAnmTable = g_stAnmSL8;
//		g_stAnmPlayPara.u16AnmTableSize = ANM_SL8_STEP_LEN;
//		Anm_Play(&g_stAnmPlayPara);
//	}
}

static void LampMode_LS9_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL9 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL9;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL9_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS27_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL27 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL27;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL27_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS28_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL28 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL28;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL28_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_LS29_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmSL29 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmSL29;
		g_stAnmPlayPara.u16AnmTableSize = ANM_SL29_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_AcReq_Handle()
{

}

static void LampMode_Goodbye1_Exit()
{
	Anm_Stop();
}
static void LampMode_Goodbye1_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}

static void LampMode_Goodbye1_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmGoodbye1 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmGoodbye1;
		g_stAnmPlayPara.u16AnmTableSize = ANM_GBY1_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_Goodbye2_Exit()
{
	Anm_Stop();
}
static void LampMode_Goodbye2_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_Goodbye2_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmGoodbye2 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmGoodbye2;
		g_stAnmPlayPara.u16AnmTableSize = ANM_GBY2_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}

static void LampMode_Goodbye3_Exit()
{
	Anm_Stop();
}
static void LampMode_Goodbye3_Enter()
{
	PWM_Capture_HV.State = PWM_NORMAL;
}
static void LampMode_Goodbye3_Handle()
{
	if(Anm_GetCurrentAnmTable() != g_stAnmGoodbye3 || PWM_LightShow.PWM_LS_Flag == ON || PWM_LightShow.PWM_LS_Sts == OFF)
	{
		Anm_Stop();
		Anm_ResetPlayPara(&g_stAnmPlayPara);
		g_stAnmPlayPara.pstAnmTable = g_stAnmGoodbye3;
		g_stAnmPlayPara.u16AnmTableSize = ANM_GBY3_STEP_LEN;
		Anm_Play(&g_stAnmPlayPara);
	}
}


static void Lamp_ModeJudge()
{
	if(PWM_Capture_HV.mode == LAMP_MODE_Not_Bright)
	{
		LAMB_Status.Not_Bright_Animation_Status = ON;
	}
	else if(PWM_Capture_HV.mode != LAMP_MODE_Not_Bright && PWM_Capture_HV.mode != LAMP_MODE_INIT)
	{
		LAMB_Status.Not_Bright_Animation_Status = OFF;
	}


	if(PWM_Capture_HV.mode != 11)
	{
		if(!g_stLinReqCmd.AnimationCtrl)
		{
			g_stLinReqCmd.AnimationCtrl = PWM_Capture_HV.mode;
		}
		if(g_stLinReqCmd.AnimationCtrl)
		{
			if((g_stLinReqCmd.AnimationCtrl != PWM_Capture_HV.mode) && PWM_Capture_HV.mode != 0)
			{
				g_stLinReqCmd.AnimationCtrl = PWM_Capture_HV.mode;
			}
		}
	}

	if(1 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_ANM1;
	}
	else if(2 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_ANM2;
	}
	else if(3 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_ANM3;
	}
	else if(4 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_GBY1;
	}
	else if(5 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_GBY2;
	}
	else if(6 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_GBY3;
	}
	else if(7 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS1;
	}
	else if(8 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS2;
	}
	else if(9 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS3;
	}
    else if(14 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS4;
	}
    else if(15 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS5;
	}
    else if(16 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS6;
	}
    else if(17 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_LS7;
	}
    else if(18 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_MS_Animation;
                Update_Animation();
	}
    else if(19 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_Not_Bright;
	}
    else if(12 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_TxReq;
	}
    else if(13 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_AcReq;
        for(uint8_t T_Count = 0; T_Count < 200; T_Count++)
        {
            *(TEST+T_Count) = *(uint8_t*)(MusicShowDownLoadStartAdress+T_Count);
        }

        if(!TEST[0] && !TEST[1])
        {
            Update_Animation();
        }

	}
    else if(11 == g_stLinReqCmd.AnimationCtrl)
	{
		g_u8ReqWorkMode = LAMP_MODE_OFF;
	}
	else
	{
		PWM_Capture_HV.mode = 0;       //add by zqf 20240719
        Anm_Stop();     //add by zqf 20240719
        Anm_ResetPlayPara(&g_stAnmPlayPara);
        Anm_Finish();
        PWM_LightShow.PWM_LS_Sts = OFF;
        g_u8ReqWorkMode = LAMP_MODE_NORMAL;
	}
}


static void Lamp_ModeChange()
{
	if(g_u8ReqWorkMode != g_u8CurWorkMode || PWM_Capture_HV.mode == LAMP_MODE_AcReq || PWM_Capture_HV.mode == LAMP_MODE_MS_Animation /*|| PWM_Capture_HV.mode == 0*/)
	{
		switch(g_u8CurWorkMode)
		{
		case LAMP_MODE_INIT:
			LampMode_Normal_Exit();
			break;
		case LAMP_MODE_NORMAL:
			LampMode_Normal_Exit();
			break;
		case LAMP_MODE_ANM1:
			LampMode_Welcome1_Exit();
			break;
		case LAMP_MODE_ANM2:
			LampMode_Welcome2_Exit();
			break;
		case LAMP_MODE_ANM3:
			LampMode_Welcome3_Exit();
			break;
		case LAMP_MODE_GBY1:
			LampMode_Goodbye1_Exit();
			break;
		case LAMP_MODE_GBY2:
			LampMode_Goodbye2_Exit();
			break;
		case LAMP_MODE_GBY3:
			LampMode_Goodbye3_Exit();
			break;
		case LAMP_MODE_LS1:
			LampMode_LS1_Exit();
			break;
		case LAMP_MODE_LS2:
			LampMode_LS2_Exit();
			break;
		case LAMP_MODE_LS3:
			LampMode_LS3_Exit();
			break;
		case LAMP_MODE_OFF:
			LampMode_OFF_Exit();
			break;
        case LAMP_MODE_LS4:
			LampMode_LS4_Exit();
			break;
        case LAMP_MODE_LS5:
			LampMode_LS5_Exit();
			break;
        case LAMP_MODE_LS6:
			LampMode_LS6_Exit();
			break;
        case LAMP_MODE_LS7:
			LampMode_LS6_Exit();
			break;
        case LAMP_MODE_MS_Animation:
			LampMode_LS8_Exit();
			break;
        case LAMP_MODE_Not_Bright:
			LampMode_LS6_Exit();
			break;
        case LAMP_MODE_AcReq:
			LampMode_LS6_Exit();
			break;
        case LAMP_MODE_LS27:
			LampMode_LS6_Exit();
			break;
        case LAMP_MODE_LS28:
			LampMode_LS6_Exit();
			break;
		default:
			break;
		}

		switch(g_u8ReqWorkMode)
		{
		case LAMP_MODE_INIT:
            LampMode_Normal_Enter();
			break;
		case LAMP_MODE_NORMAL:
			LampMode_Normal_Enter();
			break;
		case LAMP_MODE_ANM1:
			LampMode_Welcome1_Enter();
			break;
		case LAMP_MODE_ANM2:
			LampMode_Welcome2_Enter();
			break;
		case LAMP_MODE_ANM3:
			LampMode_Welcome3_Enter();
			break;
		case LAMP_MODE_GBY1:
			LampMode_Goodbye1_Enter();
			break;
		case LAMP_MODE_GBY2:
			LampMode_Goodbye2_Enter();
			break;
		case LAMP_MODE_GBY3:
			LampMode_Goodbye3_Enter();
			break;
		case LAMP_MODE_LS1:
			LampMode_LS1_Enter();
			break;
		case LAMP_MODE_LS2:
			LampMode_LS2_Enter();
			break;
		case LAMP_MODE_LS3:
			LampMode_LS3_Enter();
			break;
		case LAMP_MODE_OFF:
			LampMode_OFF_Enter();
			break;	
        case LAMP_MODE_LS4:
			LampMode_LS4_Enter();
			break;
        case LAMP_MODE_LS5:
			LampMode_LS5_Enter();
			break;
        case LAMP_MODE_LS6:
			LampMode_LS6_Enter();
			break;
        case LAMP_MODE_LS7:
			LampMode_LS3_Enter();
			break;
        case LAMP_MODE_MS_Animation:
			LampMode_LS4_Enter();
			break;
        case LAMP_MODE_Not_Bright:
			LampMode_LS5_Enter();
			break;
        case LAMP_MODE_AcReq:
			LampMode_LS6_Enter();
//                        PWM_LS_Flag = ON;
//                        PWM_LS_SW = ON;
//                        LampMode_AcReq_Handle();
			break;
        case LAMP_MODE_LS27:
			LampMode_LS5_Enter();
			break;
        case LAMP_MODE_LS28:
			LampMode_LS6_Enter();
			break;
		default:
			break;
		}
		g_u8CurWorkMode = g_u8ReqWorkMode;
	}
}


static void Lamp_ModeHandle(uint8_t l_u8CurWorkMode)
{
	switch(l_u8CurWorkMode)
	{
	case LAMP_MODE_ANM1:
		LampMode_Welcome1_Handle();
		break;
	case LAMP_MODE_ANM2:
		LampMode_Welcome2_Handle();
		break;
	case LAMP_MODE_ANM3:
		LampMode_Welcome3_Handle();
		break;
	case LAMP_MODE_GBY1:
		LampMode_Goodbye1_Handle();
		break;
	case LAMP_MODE_GBY2:
		LampMode_Goodbye2_Handle();
		break;
	case LAMP_MODE_GBY3:
		LampMode_Goodbye3_Handle();
		break;
        case LAMP_MODE_NORMAL:
		LampMode_Normal_Handle();
		break;
       case LAMP_MODE_OFF:
		LampMode_Normal_Handle();
		break;
	case LAMP_MODE_LS1:
		LampMode_LS1_Handle();
		break;
	case LAMP_MODE_LS2:
		LampMode_LS2_Handle();
		break;
	case LAMP_MODE_LS3:
		LampMode_LS3_Handle();
		break;
	case LAMP_MODE_LS4:
		LampMode_LS4_Handle();
		break;
    case LAMP_MODE_LS5:
		LampMode_LS5_Handle();
		break;
    case LAMP_MODE_LS6:
		// LampMode_LS6_Handle();
		break;
    case LAMP_MODE_LS7:
		LampMode_LS7_Handle();
		break;
	case LAMP_MODE_MS_Animation:
		LampMode_LS8_Handle();
		break;
    case LAMP_MODE_Not_Bright:
		LampMode_LS9_Handle();
		break;
    case LAMP_MODE_AcReq:
		LampMode_AcReq_Handle();
		break;
    case LAMP_MODE_INIT:
		LampMode_AcReq_Handle();
		break;

    case LAMP_MODE_LS27:
		LampMode_LS27_Handle();
		break;
    case LAMP_MODE_LS28:
		LampMode_LS28_Handle();
		break;
    case LAMP_MODE_LS29:
		LampMode_LS29_Handle();
		break;

	default:

		break;
	}
}

/*FUNCTION**********************************************************************/
void Set_A_channels0_Callbacks_0(ic_event_t event, void *userData)
{
	(void)event;
	(void)userData;
	PWM_Capture_HV.State = PWM_EN;
	PWM_Capture_HV.Clear_CcvVal_Counter_R ++;
	if(PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter >= PWM_Val_Counter_Out)
	{
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter=0;
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].OriginalCounterFinish_Flag = TRUE;
		for(uint8_t count = 0; count < PWM_Val_Counter_Out; count++)
		{
			PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Falling[count] = PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].RcvVal_Falling[count];
			PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CtestVal[count] = PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].RtestVal[count];
		}
		g_stTaskList[TASK_INDEX_INPUT].u8TaskEnFlag = 0;
	}
}

/*FUNCTION**********************************************************************/
void Set_B_channels2_Callbacks_0(ic_event_t event, void *userData)
{
	(void)event;
	(void)userData;
	PWM_Capture_HV.State = PWM_EN;
	PWM_Capture_HV.Clear_CcvVal_Counter_R ++;
	if(PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter >= PWM_Val_Counter_Out)
	{
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter=0;
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].OriginalCounterFinish_Flag = TRUE;
		for(uint8_t count = 0; count < PWM_Val_Counter_Out; count++)
		{
			PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Falling[count] = PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].RcvVal_Falling[count];
			PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CtestVal[count] = PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].RtestVal[count];
		}
		g_stTaskList[TASK_INDEX_INPUT].u8TaskEnFlag = 0;
	}
}


/*FUNCTION**********************************************************************/
status_t PWM_Register_Value_Handle_Period(volatile PWM_Capture_HVstruct *PWM_Capture_P,uint8_t Times)
{
    uint16_t getccv2 = 0;
	uint16_t val1[PWM_Val_times] = {0};
	uint16_t tval[PWM_Val_times + Times/2];
    uint8_t k = 0;
	uint8_t Pcounter = 0;
	uint8_t Dcounter2 = 0;
	uint8_t PWM_Offset = 0;
	uint8_t PWM_mode = 0;

    if(PWM_Capture_P->PWM_OV[PWM_Channel_PWM2].OriginalCounterFinish_Flag || PWM_Capture_P->PWM_OV[PWM_Channel_PWM1].OriginalCounterFinish_Flag)
    {
		if(PWM_Capture_P->PWM_OV[PWM_Channel_PWM2].OriginalCounterFinish_Flag)
		{
			for(k = 0; k< PWM_Val_times; k++)
			{
				*(val1 + k) = *(PWM_Capture_P->PWM_OV[PWM_Channel_PWM2].CcvVal_Falling + k);
				*(tval + k) = *(PWM_Capture_P->PWM_OV[PWM_Channel_PWM2].CtestVal + k);
			}
			PWM_Offset = 0;
		}

		if(PWM_Capture_P->PWM_OV[PWM_Channel_PWM1].OriginalCounterFinish_Flag)
		{
			for(k = 0; k< PWM_Val_times; k++)
			{
				*(val1 + k) = *(PWM_Capture_P->PWM_OV[PWM_Channel_PWM1].CcvVal_Falling + k);
				*(tval + k) = *(PWM_Capture_P->PWM_OV[PWM_Channel_PWM1].CtestVal + k);
			}
			PWM_Offset = 10;
		}
		PWM_Capture_P->PWM_OV[PWM_Channel_PWM2].OriginalCounterFinish_Flag = FALSE;
		PWM_Capture_P->PWM_OV[PWM_Channel_PWM1].OriginalCounterFinish_Flag = FALSE;

		for(k = 0; k < Times-1; k++)
		{
			if(val1[k+1] > val1[k])	{getccv2 = val1[k+1] - val1[k];}

			else if(val1[k+1] <= val1[k])	{getccv2 = val1[k] - val1[k+1];}

			if(getccv2 < 2000)	{Pcounter++;}

			else if(getccv2 > 16000 && getccv2 < 18181)	{Pcounter++;}
		}

		tval[PWM_Val_times + Times/2 - 1] = bubbleSort( tval, Times);
		tval[PWM_Val_times + Times/2 - 2] = tval[PWM_Val_times + Times/2 - 1];
		for(k = 0; k < Times + Times/2; k++)
		{
			if((tval[k] > 1191) && (tval[k] < 18181))
			{
				if(tval[k] > 1191 && tval[k] < 2645)		{PWM_Capture_P->pwm_counter[PWM_Offset + 1]++;}
				else if(tval[k] < 4463 && tval[k] > 3009)	{PWM_Capture_P->pwm_counter[PWM_Offset + 2]++;}
				else if(tval[k] < 6281 && tval[k] > 4827)	{PWM_Capture_P->pwm_counter[PWM_Offset + 3]++;}
				else if(tval[k] < 8100 && tval[k] > 6645)	{PWM_Capture_P->pwm_counter[PWM_Offset + 4]++;}
				else if(tval[k] < 9918 && tval[k] > 8463)	{PWM_Capture_P->pwm_counter[PWM_Offset + 5]++;}
				else if(tval[k] < 11736 && tval[k] > 10281)	{PWM_Capture_P->pwm_counter[PWM_Offset + 6]++;}
				else if(tval[k] < 13554 && tval[k] > 12100)	{PWM_Capture_P->pwm_counter[PWM_Offset + 7]++;}
				else if(tval[k] < 15372 && tval[k] > 13918)	{PWM_Capture_P->pwm_counter[PWM_Offset + 8]++;}
				else if(tval[k] < 17191 && tval[k] > 15736)	{PWM_Capture_P->pwm_counter[PWM_Offset + 9]++;}
				else 										{PWM_Capture_P->pwm_counter[0]++;}
			}
		}

		for (uint8_t m = 0; m < 20; m++)  // 10%->20% dutycycle count clear
		{
			if(PWM_Capture_P->pwm_counter[m] > Dcounter2)
			{
				Dcounter2 = PWM_Capture_P->pwm_counter[m];
				PWM_mode = m;
			}
		}

		if(Pcounter < (Times - Times/2)) {PWM_mode = 0;}

		if(Dcounter2 <= Times/2)	{PWM_mode = 0;}

		for (uint8_t n = 0; n < 20; n++)   //10->20
		{
			PWM_Capture_P->pwm_counter[n] = 0;
		}


		PWM_Capture_P->PWM_OV[PWM_Channel_PWM2].CcvVal_Counter = 0;
		PWM_Capture_P->PWM_OV[PWM_Channel_PWM1].CcvVal_Counter = 0;

		
		if((PWM_mode != 0) && (PWM_mode != 7) && (PWM_mode != 8) && (PWM_mode != 9) && (PWM_mode != 12) && (PWM_mode != 13)  
			&& (PWM_mode != 14) && (PWM_mode != 15) && (PWM_mode != 17) &&(PWM_mode != 19))
		{
			PWM_Capture_P->mode = PWM_mode;
			for(uint8_t j = 0; j < Times + Times/2; j++)
			{
				tval[j] = 0;
			}
		}

		if((PWM_mode != 0) && (PWM_mode != 7) && (PWM_mode != 8) && (PWM_mode != 9) && (PWM_mode != 12) && (PWM_mode != 13)  
			&& (PWM_mode != 14) && (PWM_mode != 15) && (PWM_mode != 17) &&(PWM_mode != 19))
		{
	    	PWM_Mode_Handler_Special();
		}
    }
    return STATUS_SUCCESS;
}

/*FUNCTION**********************************************************************/
void PWM_Mode_Handler_LS(void)
{
	if((PWM_LightShow.PWM_LS_Flag == ON) || (PWM_LightShow.PWM_LS_SW && PWM_Capture_HV.mode == 0))
	{
		if(PWM_LightShow.PWM_LS_Flag)
		{
			Update_Animation();
			PWM_LightShow.PWM_LS_Flag = OFF;
		}

	    PWM_Capture_HV.mode = PWM_LightShow.DL.TEST[PWM_LightShow.PWM_LS_Counter];

	    if(PWM_LightShow.DL.TEST[PWM_LightShow.PWM_LS_Counter] == 0)       //When LS is finish,change the LS Flag to inactive
	    {
	        for(uint8_t T_Count = 0; T_Count < 200; T_Count++)
	        {
//	            *(PWM_LightShow.DL.TEST+T_Count) = *(uint8_t*)(0x01000000+T_Count);
	        }
	        PWM_LightShow.PWM_LS_SW = OFF;
	        PWM_LightShow.PWM_LS_Flag = OFF;
	        PWM_Capture_HV.mode = 0;
	        PWM_LightShow.PWM_LS_Counter = 0;
	        Anm_Finish();
	    }
	    g_stTaskList[TASK_INDEX_LOGIC].u8TaskEnFlag = 1;
	    g_stTaskList[TASK_INDEX_LOGIC].u32DelayTime = 0;

	    PWM_LightShow.PWM_LS_Counter++;

	}

	else
		{g_u8LS_Start_Wait = OFF;}
}


/*FUNCTION**********************************************************************/
void PWM_Mode_Handler_Special(void)
{

	if(PWM_Capture_HV.mode == 16)
	{
		g_u8P_S_Flag = ON;
		Anm_Finish();
		PWM_Capture_HV.mode = 0;
	}
	if(PWM_Capture_HV.mode == LAMP_MODE_OFF)
	{
		g_u8P_S_Flag = OFF;
	    Anm_Finish();
	    if(PWM_LightShow.DL.PWM_SAVE_FLAG == 1)
	    {
	    	PWM_LightShow.DL.PWM_SAVE_FLAG = 2;
	    	PWM_LightShow.DL.Download_Count = 0;
	    }
		//save finish flag -> go to Memory handle
	    PWM_Capture_HV.mode = 0;
	}

	if(PWM_LightShow.DL.PWM_SAVE_FLAG == 1)
	{
	    PWM_LightShow.DL.PWM_SAVE_RAM[PWM_LightShow.DL.Download_Count++] = PWM_Capture_HV.mode;
	    PWM_Capture_HV.mode = 0;
	}
	/**************PWM_DOWNLOAD******************/
	if(PWM_Capture_HV.mode == LAMP_MODE_TxReq)
	{
	    PWM_LightShow.DL.PWM_SAVE_FLAG = 1;     //saving flag
	    PWM_Capture_HV.mode = 0;
	}

	/**********************************/
    if((PWM_Capture_HV.mode == LAMP_MODE_AcReq || PWM_Capture_HV.mode == LAMP_MODE_MS_Animation) && PWM_LightShow.PWM_LS_Flag == OFF)
    {
    	PWM_LightShow.PWM_LS_SW = ON;
    	PWM_LightShow.PWM_LS_Flag = ON;
    }

    else if(PWM_Capture_HV.mode != LAMP_MODE_AcReq && PWM_Capture_HV.mode != LAMP_MODE_MS_Animation)
    {
    	PWM_LightShow.PWM_LS_SW = OFF;
    	PWM_LightShow.PWM_LS_Flag = OFF;

    }

    g_stTaskList[TASK_INDEX_GENERAL].u32DelayTime= 0;    //start animation right now
    g_stTaskList[TASK_INDEX_GENERAL].u8TaskEnFlag = ENABLE;
}

/*FUNCTION**********************************************************************/
void Clear_PWM_InputCapture_OV_C(void)
{
	if(PWM_Capture_HV.Clear_CcvVal_Counter_C == PWM_Capture_HV.Clear_CcvVal_Counter_R)
	{
		PWM_Capture_HV.State = PWM_NORMAL;
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter = 0;
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter = 0;
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].OriginalCounterFinish_Flag = 0;
		PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].OriginalCounterFinish_Flag = 0;
	}
	PWM_Capture_HV.Clear_CcvVal_Counter_C = PWM_Capture_HV.Clear_CcvVal_Counter_R;
}

/*FUNCTION**********************************************************************/
void ADC_Update(void)
{
	pins_channel_type_t swipe_turn;
#if CURRENT_PROJECT  == PROJECT_trunk
	swipe_turn = (PINS_DRV_ReadPins(PTE)&(1<<9));
	LAMB_Status.DI_Left_ADC_Status = Turn_Left_ADC();
	LAMB_Status.DI_Right_ADC_Status = Turn_Right_ADC();
	LAMB_Status.PO_Left_ADC_Status = Tail_Left_ADC();
	LAMB_Status.PO_Right_ADC_Status = Tail_Right_ADC();
	LAMB_Status.Swipe_Turn_Signal = (uint8_t)(swipe_turn >> 9);
	
	if(LAMB_Status.PO_Left_ADC_Status)
	{
		if(LAMB_Status.PO_Left_ADC_Count_H < 10)
		{
			LAMB_Status.PO_Left_ADC_Count_H++;
		}
	}else
	{
		LAMB_Status.PO_Left_ADC_Count_H = 0;
	}

	if(LAMB_Status.PO_Right_ADC_Status)
	{
		if(LAMB_Status.PO_Right_ADC_Count_H < 10)
		{
			LAMB_Status.PO_Right_ADC_Count_H++;
		}
	}else
	{
		LAMB_Status.PO_Right_ADC_Count_H = 0;
	}

	if(LAMB_Status.DI_Left_ADC_Status)
	{
		if(LAMB_Status.DI_Left_ADC_Count_H < 3)
		{
			LAMB_Status.DI_Left_ADC_Count_H++;
		}
	}else
	{
		LAMB_Status.DI_Left_ADC_Count_H = 0;
	}

	if(LAMB_Status.DI_Right_ADC_Status)
	{
		if(LAMB_Status.DI_Right_ADC_Count_H < 3)
		{
			LAMB_Status.DI_Right_ADC_Count_H++;
		}
	}else
	{
		LAMB_Status.DI_Right_ADC_Count_H = 0;
	}
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
	swipe_turn = (PINS_DRV_ReadPins(PTE)&(1<<9));
	LAMB_Fender_Status.PO_ADC_Status = Tail_ALight_ADC();
	LAMB_Fender_Status.DI_ADC_Status = Turn_ALight_ADC();
	LAMB_Fender_Status.ST_ADC_Status = Stop_ALight_ADC();
	LAMB_Fender_Status.Swipe_Turn_Signal = (uint8_t)(swipe_turn >> 9);
	
	if(LAMB_Fender_Status.PO_ADC_Status)
	{
		if(LAMB_Fender_Status.PO_ADC_Count_H < 10)
		{
			LAMB_Fender_Status.PO_ADC_Count_H++;
		}
	}else
	{
		LAMB_Fender_Status.PO_ADC_Count_H = 0;
	}

	if(LAMB_Fender_Status.DI_ADC_Status)
	{
		if(LAMB_Fender_Status.DI_ADC_Count_H < 3)
		{
			LAMB_Fender_Status.DI_ADC_Count_H++;
		}
	}else
	{
		LAMB_Fender_Status.DI_ADC_Count_H = 0;
	}

	if(LAMB_Fender_Status.ST_ADC_Status)
	{
		if(LAMB_Fender_Status.ST_ADC_Count_H < 3)
		{
			LAMB_Fender_Status.ST_ADC_Count_H++;
		}
	}else
	{
		LAMB_Fender_Status.ST_ADC_Count_H = 0;
	}
#endif
}


/*FUNCTION**********************************************************************/
void Light_Update(void)
{
	static uint8_t Task_Cnt = 0;
	static uint8_t Loop_Flag = 1;
	uint8_t pwmduty_8byte[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
	uint8_t pwmduty_4byte[4] = {0x0, 0x0, 0x0, 0x0};
//	static uint8_t Lamps_Fender_Status = OFF;

#if CURRENT_PROJECT  == PROJECT_trunk
	if(LAMB_Status.DI_Left_ADC_Status || LAMB_Status.DI_Right_ADC_Status || LAMB_Status.PO_Left_ADC_Status || LAMB_Status.PO_Right_ADC_Status)
	{
		if(PWM_Capture_HV.mode)
		{
			if(s_u8Lamps_Status == OFF)
			{
				Loop_Flag = 0;
				Tps_BC_SetLock(0xFF, 0);
				Tps_BC_SetMISC4(0xFF, 0);
				Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
				// Tps_BC_SetChannelPWMDuty(0xFF, 0, 0x00);
				Tps_BC_SetMISC7(0xFF, 0x00);
				Tps_BC_SetMISC8(0xFF, 0xC8);
				Tps_BC_SetCLR(0xFF, 0x3);
				Tps_BC_EnableAllChannel();
				s_u8Lamps_Status = ON;
			}
		}
		else
		{
			if(s_u8Lamps_Status == OFF)
			{
				Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
				Tps_BC_EnableAllChannel();
				Tps_BC_SetChannelPWMDuty(0xFF, 0, 0x00);
				Tps_BC_SetMISC7(0xFF, 0x10);
				Tps_BC_SetMISC8(0xFF, 0xC8);
				Tps_BC_SetCLR(0xFF, 0x3);
				Tps_BC_SetLock(0xFF, 0);
				Tps_BC_SetMISC7(0xFF, 0x00);
				s_u8Lamps_Status = ON;
			}
			else
			{
				if(Task_Cnt  == 0)
				{
					Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
					Tps_BC_EnableAllChannel();
					Tps_WriteOneRegBlock(TPS_CHIP_ADD_14, REG_ADD_CFG_DIAGEN0, 0xFF, TRUE);	//Enable Diag Register
					Tps_WriteOneRegBlock(TPS_CHIP_ADD_14, REG_ADD_CFG_DIAGEN1, 0x3, TRUE);
				}
				if(Task_Cnt  == 1)
				{
					Tps_WriteOneRegBlock(TPS_CHIP_ADD_15, REG_ADD_CFG_DIAGEN0, 0xFF, TRUE);	
					Tps_WriteOneRegBlock(TPS_CHIP_ADD_15, REG_ADD_CFG_DIAGEN1, 0x3, TRUE);
					Tps_BC_SetLock(0xFF, 0);
					Tps_BC_SetMISC7(0xFF, 0x00);

				}
				if(Task_Cnt  == 2)
				{
					Tps_BC_SetCLR(0xFF, 0x3);
					Tps_BC_SetMISC4(0xFF, 0);
					Tps_BC_SetMISC8(0xFF, 0xC8);
					
				}
			}
		}

		if(Loop_Flag )
		{
			if(!PWM_LightShow.PWM_LS_SW)
			{
				TPS_PWM_UPDATE(g_16SetPwmDuty);
			}
			else if(PWM_LightShow.PWM_LS_SW && !g_u8LS_Start_Wait )
			{
				TPS_PWM_UPDATE(g_16SetPwmDuty);
			}
			
		}
		if(!Loop_Flag)	Loop_Flag = 1;
		Task_Cnt++;
		if(Task_Cnt > 2) Task_Cnt = 0;
	}
	else
	{
		Task_Cnt = 0;
		s_u8Lamps_Status = OFF;
	}
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
	if(LAMB_Fender_Status.DI_ADC_Status || LAMB_Fender_Status.PO_ADC_Status || LAMB_Fender_Status.ST_ADC_Status)
	{
		// if(Task_Cnt % 6 < 3)
		{
			//init 0x60 0x61
			if(PWM_Capture_HV.mode)
			{
				if(s_u8Lamps_Status == OFF)
				{
					Loop_Flag = 0;

					Tps_BC_SetLock(0xFF, 0);
					Tps_BC_SetMISC4(0xFF, 0);
					Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
					Tps_BC_SetMISC0(0xFF, 0);
					Tps_BC_SetMISC7(0xFF, 0x00);
					Tps_BC_SetMISC8(0xFF, 0xC8);
					Tps_BC_SetCLR(0xFF, 0x3);
					Tps_BC_EnableAllChannel();
					s_u8Lamps_Status = ON;
				}

			}
			else
			{
				if(s_u8Lamps_Status == OFF)
				{
					Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
					Tps_BC_EnableAllChannel();
					Tps_BC_SetChannelPWMDuty(0xFF, 0, 0x00);
					Tps_BC_SetMISC7(0xFF, 0x10);
					Tps_BC_SetMISC8(0xFF, 0xC8);
					Tps_BC_SetCLR(0xFF, 0x3);
					Tps_BC_SetLock(0xFF, 0);
					Tps_BC_SetMISC7(0xFF, 0x00);
					s_u8Lamps_Status = ON;
				}
				else
				{
					if(Task_Cnt == 0)
					{
						
						Tps_BC_SetPwmReqAndRefRang(0xFF, PWM_FREQ_7800HZ, REFRANG_512);
						Tps_BC_EnableAllChannel();
					}
					if(Task_Cnt == 1)
					{
						Tps_WriteOneRegBlock(TPS_CHIP_ADD_2, REG_ADD_CFG_DIAGEN0, 0xFF, TRUE);	//Enable Diag Register
						Tps_WriteOneRegBlock(TPS_CHIP_ADD_2, REG_ADD_CFG_DIAGEN1, 0x3, TRUE);
						Tps_BC_SetLock(0xFF, 0);
						Tps_BC_SetMISC7(0xFF, 0);

					}
					if(Task_Cnt == 2)
					{
						Tps_BC_SetCLR(0xFF, 0x3);
						Tps_BC_SetMISC4(0xFF, 0);
						Tps_BC_SetMISC8(0xFF, 0xC8);
					}
				}
				

				Task_Cnt++;
				if(Task_Cnt > 2) Task_Cnt = 0;
			}

		}


		if(Loop_Flag)
		{
			if(!PWM_LightShow.PWM_LS_SW)
			{
				TPS_PWM_UPDATE(g_16FenderSetPwmDuty);
			}
			else if(PWM_LightShow.PWM_LS_SW && !g_u8LS_Start_Wait )
			{
				TPS_PWM_UPDATE(g_16FenderSetPwmDuty);
			}
			
		}

		if(!Loop_Flag)	Loop_Flag = 1;
		
	}

	else
	{
		Task_Cnt = 0;
		s_u8Lamps_Status = OFF;
	}
#endif
}

/*FUNCTION**********************************************************************/
void HALL_50ms(void)
{
	static uint8_t normal_cnt = 0;
	if(g_stTaskList[TASK_INDEX_GENERAL].u32CycleTime == 50)
	{
		if(PWM_Capture_HV.State == PWM_DIS) //PWM_DIS
		{
#if CURRENT_PROJECT  == PROJECT_trunk
			if(LAMB_Status.PO_Left_ADC_Count_H >= 8) 
			{
				if(PWM_Capture_HV.mode == 0)
				{
					PO_Left_On();
				}
				LAMB_Status.PO_Left_ADC_Count_H = 1;
			}
			if(LAMB_Status.PO_Left_ADC_Count_H == 0)
				{
					PO_Left_Off();
				}


			if(LAMB_Status.PO_Right_ADC_Count_H >= 8)
			{
				PO_Right_On();
				LAMB_Status.PO_Right_ADC_Count_H = 1;
				PWM_Capture_HV.State = PWM_NORMAL;
			}
			if(LAMB_Status.PO_Right_ADC_Count_H == 0)
			{
				PO_Right_Off();
			}

#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
			if(LAMB_Fender_Status.PO_ADC_Count_H >= 9)
			{
				PO_ALight_On();
				LAMB_Fender_Status.PO_ADC_Count_H = 1;
			}
			if(LAMB_Fender_Status.PO_ADC_Count_H == 0)
			{
				PO_ALight_Off();
			}

			if(LAMB_Fender_Status.ST_ADC_Count_H > 1)
			{
				Stop_On_Fender();
				LAMB_Fender_Status.ST_ADC_Count_H = 1;
			}
			if(LAMB_Fender_Status.ST_ADC_Count_H == 0)
			{
				Stop_Off_Fender();
			}
#endif
			PWM_Capture_HV.State = PWM_NORMAL;
			g_stTaskList[TASK_INDEX_GENERAL].u32CycleTime = 60;
		}
		else if((PWM_Capture_HV.State == PWM_EN) && (PWM_Capture_HV.mode == LAMP_MODE_INIT)) //PWM_EN
		{
			normal_cnt++;
			if(normal_cnt > 1)
			{
				normal_cnt = 0;
				PWM_Capture_HV.State = PWM_NORMAL;
				g_stTaskList[TASK_INDEX_GENERAL].u32CycleTime = 60;
			}
		}
		else
		{
			g_stTaskList[TASK_INDEX_GENERAL].u32CycleTime = 60;
		}

#if CURRENT_PROJECT  == PROJECT_trunk
		if((LAMB_Status.PO_Left_ADC_Count_H == 0) && (LAMB_Status.PO_Right_ADC_Count_H == 0))
		{
			PWM_Capture_HV.State = PWM_DIS;
		}
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
		if(LAMB_Fender_Status.PO_ADC_Count_H == 0)
		{
			PWM_Capture_HV.State = PWM_DIS;
		}
#endif
	}

	if(g_stTaskList[TASK_INDEX_GENERAL].u32CycleTime == 60)
	{
		if((PWM_Capture_HV.mode == 0) || (PWM_Capture_HV.mode == 16))
		{
#if CURRENT_PROJECT  == PROJECT_trunk
			if(LAMB_Status.Not_Bright_Animation_Status!= ON)
			{
				if(LAMB_Status.PO_Left_ADC_Count_H >= 8 || g_u8P_S_Flag)
				{
					PO_Left_On();
					LAMB_Status.PO_Left_ADC_Count_H = 1;
					PWM_Capture_HV.State = PWM_NORMAL;
				}
				
				if(LAMB_Status.PO_Left_ADC_Count_H == 0)
				{
					PO_Left_Off();
				}


				if(LAMB_Status.PO_Right_ADC_Count_H >= 8 || g_u8P_S_Flag)
				{
					PO_Right_On();
					LAMB_Status.PO_Right_ADC_Count_H = 1;
					PWM_Capture_HV.State = PWM_NORMAL;
				}
				if(LAMB_Status.PO_Right_ADC_Count_H == 0)
				{
					PO_Right_Off();
				}
			}
		
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
			if(LAMB_Fender_Status.Not_Bright_Animation_Status!= ON)
			{
				if(LAMB_Fender_Status.PO_ADC_Count_H >= 9 || g_u8P_S_Flag)
				{
					PO_ALight_On();
					LAMB_Fender_Status.PO_ADC_Count_H = 1;
					PWM_Capture_HV.State = PWM_NORMAL;
				}

				if(LAMB_Fender_Status.PO_ADC_Count_H == 0)
				{
					PO_ALight_Off();
				}

				if(LAMB_Fender_Status.ST_ADC_Count_H > 1)
				{
					Stop_On_Fender();
					LAMB_Fender_Status.ST_ADC_Count_H = 1;
				}
				if(LAMB_Fender_Status.ST_ADC_Count_H == 0)
				{
					if(g_u8P_S_Flag)
					{
						Safety_LightOn();
					}
					else
					{
						Stop_Off_Fender();
					}
				}
			}
#endif
		}
	}
}

/*FUNCTION**********************************************************************/
void LED_SetDuty(uint8_t u8dev, uint8_t u8chn, uint16_t u16duty_chn)
{
#if CURRENT_PROJECT  == PROJECT_trunk

	if((PWM_Capture_HV.mode == 4) || (PWM_Capture_HV.mode == 1))
	{
		if(u8dev == TPS_CHIP_ADD_0)
		{
			for(uint8_t i=0; i<8; i++)
			{
				g_16SetPwmDuty[TPS_CHIP_ADD_0][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_1][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_2][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_3][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_4][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_5][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_6][i] = u16duty_chn;
				g_16SetPwmDuty[TPS_CHIP_ADD_7][i] = u16duty_chn;
			}
		}
		
		if(u8dev >= TPS_CHIP_ADD_8)
		{
			g_16SetPwmDuty[u8dev][u8chn] = u16duty_chn;
		}
	}
	else
	{
		g_16SetPwmDuty[u8dev][u8chn] = u16duty_chn;
	}
	
	
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))

	if((PWM_Capture_HV.mode == 4) || (PWM_Capture_HV.mode == 1))
	{
		if(u8chn == 0)
		{
			for(uint8_t i=0; i<6; i++)
			{
				g_16FenderSetPwmDuty[u8dev][i] = u16duty_chn;	
			}
		}
		else
		{
			g_16FenderSetPwmDuty[u8dev][u8chn] = u16duty_chn;
		}
	}
	else
	{
		g_16FenderSetPwmDuty[u8dev][u8chn] = u16duty_chn;
	}
	
	
#endif
}




/*FUNCTION**********************************************************************/
void Task_LightLogic_Handler()
{
    if(g_stTaskList[TASK_INDEX_GENERAL].u8TaskEnFlag)
    {
        g_stTaskList[TASK_INDEX_GENERAL].u8TaskEnFlag = DISABLE;
        Lamp_ModeJudge();
        Lamp_ModeChange();
        Lamp_ModeHandle(g_u8CurWorkMode);
    }
}

/*
// Add IN "ftm_ic_driver.c" function:FTM_DRV_InputCaptureHandler #include "General.h" counter time out vale
        if(!channel)
        {
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].RcvVal_Rising[PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter] = first_event_time;
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].RcvVal_Falling[PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter] = second_event_time;
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].RtestVal[PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter] = state->measurementResults[channel];
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM1].CcvVal_Counter++;
        }
        else if(channel)
        {
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].RcvVal_Rising[PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter] = first_event_time;
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].RcvVal_Falling[PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter] = second_event_time;
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].RtestVal[PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter] = state->measurementResults[channel];
        	PWM_Capture_HV.PWM_OV[PWM_Channel_PWM2].CcvVal_Counter++;
        }
*/
